package com.qinhao.loveCrowdFunding.service;

import com.qinhao.loveCrowdFunding.controller.AdminController;
import com.qinhao.loveCrowdFunding.controller.FundingInformationController;
import com.qinhao.loveCrowdFunding.mapper.AdminMapper;
import com.qinhao.loveCrowdFunding.pojo.*;
import com.qinhao.loveCrowdFunding.req.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class AdminService {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    FundingInformationController fundingInformationController;
    @Autowired
    AdminController adminController;
    @Autowired
    IntegralService integralService;
    public AdminControlInformationDTO getInformation() {
        AdminControlInformationDTO adminControlInformationDTO = AdminControlInformationDTO.builder().build();
        List<LoveCrowdSimpleInformation> unapproved = adminMapper.getUnapproved();
        List<LoveCrowdSimpleInformation> reported = adminMapper.getReported();
        // 获取每个被举报众筹项目被举报的原因
        if (reported != null) {
            for (int i = 0; i < reported.size(); i++) {
                List<BeReportedReasonBO> beReportedReasonBOList = adminMapper.getBeReportedReasonBySimpleInformationId(reported.get(i));
                StringBuilder stringBuilder = new StringBuilder();
                for (int j = 0; j < beReportedReasonBOList.size(); j++) {
                    if (j != 0) {
                        stringBuilder.append(";");
                    }
                    stringBuilder.append(beReportedReasonBOList.get(j).getReportContent());
                }
                reported.get(i).setReportContent(stringBuilder.toString());
            }
        }
        List<LoveCrowdSimpleInformation> all = adminMapper.getAll();
        adminControlInformationDTO.setUnapproved(unapproved);
        adminControlInformationDTO.setReported(reported);
        // 改变all的fundingStatus的文案
        if (all != null) {
            for (int i = 0; i < all.size(); i++) {
                all.get(i).setFundingStatus(fundingInformationController.getStatus(Integer.parseInt(all.get(i).getFundingStatus())));
            }
        }
        adminControlInformationDTO.setAll(all);
        return adminControlInformationDTO;
    }

    public void rejectReview(RejectReviewRequest rejectReviewRequest) {
        adminMapper.rejectReview(rejectReviewRequest);
    }

    public void approveReview(ApproveReviewRequest approveReviewRequest) throws ParseException {
        adminMapper.approveReview(approveReviewRequest);
        String endDate = getEndDate(approveReviewRequest.getDate(), approveReviewRequest.getFundingCreateTime());
        ApproveReviewEntity approveReviewEntity = ApproveReviewEntity.builder().build();
        approveReviewEntity.setDate(endDate);
        approveReviewEntity.setId(approveReviewRequest.getId());
        adminMapper.setEndDateById(approveReviewEntity);
    }

    public String getEndDate(String date, String fundingCreateTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = sdf.parse(fundingCreateTime);
        sdf.format(parse);
        Calendar ca = sdf.getCalendar();
        if (date.equals("一周")) {
            ca.add(Calendar.DAY_OF_WEEK, 7);
        }else if (date.equals("一个月")) {
            ca.add(Calendar.MONTH, 1);
        }else if (date.equals("三个月")) {
            ca.add(Calendar.MONTH, 3);
        }else if (date.equals("半年")) {
            ca.add(Calendar.MONTH, 6);
        }else {
            ca.add(Calendar.YEAR, 1);
        }
        System.out.println(ca.getTime());
        return sdf.format(ca.getTime());
    }

    @Transactional
    public void rejectReport(RejectReportRequest rejectReportRequest) {
        // 更改状态为1
        adminMapper.convertStatusToPublishBySimpleInformationId(rejectReportRequest);
        // 获取举报人的id
        List<ReportUserIdBO> reportUserId = adminMapper.getReportUserId(rejectReportRequest);
        // 发送举报失败的消息
        String reportFailMessage = "你举报的标题为" + rejectReportRequest.getTitle() + "的众筹内容举报失败,原因为没有发现违规原因";
        for (int i = 0; i < reportUserId.size(); i++) {
            adminController.sendMessage(reportFailMessage, reportUserId.get(i).getReportUserId());
            // 举报该项目的人扣除5积分
            integralService.addIntegralChange(-5, reportUserId.get(i).getReportUserId(), "举报标题为" + rejectReportRequest.getTitle() + "的项目失败");
        }
        // 删除举报内容
        adminMapper.deleteReportContentBySimpleInformationId(rejectReportRequest);
    }

    @Transactional
    public void approveReport(ApproveReportRequest approveReportRequest) {
        // 更改状态为5
        adminMapper.convertStatusToAbnormalEndBySimpleInformationId(approveReportRequest);
        // 获取举报人的id
        RejectReportRequest rejectReportRequest = RejectReportRequest.builder().simpleInformationId(approveReportRequest.getSimpleInformationId()).build();
        List<ReportUserIdBO> reportUserId = adminMapper.getReportUserId(rejectReportRequest);
        // 发送举报成功的消息
        String reportFailMessage = "你举报的标题为" + approveReportRequest.getTitle() + "的众筹内容举报成功";
        for (int i = 0; i < reportUserId.size(); i++) {
            adminController.sendMessage(reportFailMessage, reportUserId.get(i).getReportUserId());
            // 举报该项目的人加上5积分
            integralService.addIntegralChange(5, reportUserId.get(i).getReportUserId(), "举报标题为" + approveReportRequest.getTitle() + "的项目成功");
        }
        // 被举报用户积分扣除
        integralService.addIntegralChange(-5, approveReportRequest.getUserId(), "你的标题为" + approveReportRequest.getTitle() + "的项目被举报");
    }
    // 强制下线
    public void forcedOffline(ForcedOfflineRequest forcedOfflineRequest) {
        ApproveReportRequest approveReportRequest = ApproveReportRequest.builder().simpleInformationId(forcedOfflineRequest.getId()).build();
        // 项目被强制下线积分扣除
        integralService.addIntegralChange(-5, forcedOfflineRequest.getUserId(), "你的标题为" + forcedOfflineRequest.getTitle() + "的项目被强制下线");
    }

}
